//
//  topLeft.m
//  ILiangCang
//
//  Created by ZX on 16-4-18.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "topLeft.h"
#import "Masonry.h"
#import "httpEngine.h"
#import "UIImageView+WebCache.h"
#import "infosModel.h"
#import "leftCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface topLeft ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray * dataArray;

@end

@implementation topLeft

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self addCollectionView];
    
}

-(void)addCollectionView
{
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 20;
    layout.itemSize = CGSizeMake(WIDTH/2-40, WIDTH/3-16);
    layout.sectionInset = UIEdgeInsetsMake(WIDTH/2-20, 25, 10, 25);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49-15-64) collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor colorWithRed:185.0/255 green:210.0/255 blue:220.0/255 alpha:1];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[leftCell class] forCellWithReuseIdentifier:@"cell"];
    
    UIButton * collectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    collectBtn.frame = CGRectMake(WIDTH/13, 20, WIDTH/2-40, WIDTH/3-16);
    [collectBtn setBackgroundImage:[UIImage imageNamed:@"favour_bg"] forState:UIControlStateNormal];
    [collectionView addSubview:collectBtn];
    UIImageView * icon1 = [[UIImageView alloc]init];
    icon1.image = [ UIImage imageNamed:@"favour_icon"];
    [collectBtn addSubview:icon1];
    [icon1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(collectBtn.mas_centerY);
        make.left.mas_equalTo(collectBtn.mas_left).with.offset(22);
    }];
    UILabel * lab1 = [[UILabel alloc]init];
    lab1.text = @"我的收藏";
    lab1.font = [UIFont systemFontOfSize:13];
    lab1.textColor = [UIColor whiteColor];
    lab1.textAlignment = NSTextAlignmentLeft;
    [collectBtn addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(collectBtn.mas_centerY);
        make.left.mas_equalTo(collectBtn.mas_left).with.offset(48);
    }];
    
    
    
    
    UIButton * magazineBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    magazineBtn.frame = CGRectMake(15+WIDTH/2, 20, WIDTH/2-40, WIDTH/3-16);
    [magazineBtn setBackgroundImage:[UIImage imageNamed:@"all_bg"] forState:UIControlStateNormal];
    [collectionView addSubview:magazineBtn];
    
    UIImageView * icon2 = [[UIImageView alloc]init];
    icon2.image = [ UIImage imageNamed:@"all_icon"];
    [magazineBtn addSubview:icon2];
    [icon2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(magazineBtn.mas_centerY);
        make.left.mas_equalTo(magazineBtn.mas_left).with.offset(22);
    }];
    UILabel * lab2 = [[UILabel alloc]init];
    lab2.text = @"所有杂志";
    lab2.font = [UIFont systemFontOfSize:13];
    lab2.textColor = [UIColor whiteColor];
    lab2.textAlignment = NSTextAlignmentLeft;
    [magazineBtn addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(magazineBtn.mas_centerY);
        make.left.mas_equalTo(magazineBtn.mas_left).with.offset(48);
    }];
    
    [httpEngine requestToGetMagazineLeftCompletion:^(NSMutableArray *leftArray, NSError *error) {
        _dataArray = leftArray;
        [collectionView reloadData];
    }];

    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    leftCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    infosModel * info = _dataArray[indexPath.item];
    [cell.coverImage sd_setImageWithURL:info.thumb];
    cell.title.text = info.catName;
    
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
